package com.atguigu.gulimall.product.web;

import com.atguigu.common.constant.AuthConstant;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.Category2Vo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
public class IndexController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 查询所有一级分类
     * @param model
     * @return
     */
    @GetMapping({"/","/index.html"})
    public String indexPage(Model model){
        //1.查出所有一级分类
        List<CategoryEntity> categoryEntityList=categoryService.getLevel1Categorys();

        model.addAttribute("categorys",categoryEntityList);
        return "index";
    }

    /**
     * 查询所有一级分类对应的2,3级子分类
     * @return
     */
    @ResponseBody
    @GetMapping("/index/json/catalog.json")
    public Map<String,List<Category2Vo>> getCategoryJson(){
        Map<String,List<Category2Vo>> map=categoryService.getCategoryJson();
        System.out.println("一级分类对应的2,3级子分类");
        System.out.println(map);
        return map;
    }

    /**
     * 退出登录
     * @param session
     * @return
     */
    @GetMapping("/tuichu")
    public String tuiChu(HttpSession session){
//        session.invalidate();
        session.removeAttribute(AuthConstant.LOGIN_USER);

        return "redirect:http://localhost:20000/login.html";
    }
}
